/**
 * @file    TestBin.cpp
 * @brief   Binary array data input test
 * @date    2014/02
 * @version
 * Copyright (c)
 */

#include <iostream>
#include "BinInput.cpp"

int main()
{
	{
		std::string casename = "BO3";
		std::cout << "Scanning the binary output file of case \"" << casename << "\"" << std::endl;

		BinReader Rpt;
		Rpt.Scan(casename);

		std::cout << "Overall grid size=" << Rpt.GrossSize()
				  << ", active grid=" << Rpt.NetSize()
				  << ", number of time step=" << Rpt.NStep()
				  << "\n"
				  << std::endl;

		// show all properties
		std::cout << "Grid static properties: " << std::endl;
		for (const Name_Loc &info : Rpt.stat_props)
		{
			std::cout << "Name: " << info.name << "\t";
			std::cout << "Length: " << info.length << "\t";
			std::cout << "Unit: " << info.unit << "\t";
			std::cout << "is at cell center: " << info.on_grid << std::endl;
		}

		{ /* Reservoir grid poly data */
			std::cout << std::endl
					  << "basic xyz postions: " << std::endl;
			double x0, y0, d0;
			Rpt.GetAxisOffset(x0, y0, d0);
			std::cout << "Axis_offset=(" << x0 << ", " << y0 << ", " << d0 << ")" << std::endl;
			std::cout << "NI=" << Rpt.SGridNI() << "\t";
			std::cout << "NJ=" << Rpt.SGridNJ() << "\t";
			std::cout << "NK=" << Rpt.SGridNK() << "\t";

			PropArray GRID_VTX_array;
			bool find_GRID_VTX = Rpt.GetStaticData("GRID_VTX", GRID_VTX_array);

			//////////// 打印节点数组
			std::cout << std::endl
					  << "## GRID_VTX:" << std::endl;
			if (find_GRID_VTX)
			{
				std::cout << "Find GRID_VTX, length=" << GRID_VTX_array.length << std::endl;
				std::cout << "MAX=" << GRID_VTX_array.Max << "\t";
				std::cout << "MIN=" << GRID_VTX_array.Min << "\t";
				std::cout << "AVG=" << GRID_VTX_array.Avg << "\t" << std::endl;
				std::cout << "data-type=" << GRID_VTX_array.type << "\t";
				std::cout << "time-type=" << GRID_VTX_array.time_type << "\t";
				std::cout << "time=" << GRID_VTX_array.time << "\t";
				std::cout << "data-unit=" << GRID_VTX_array.unit << "\t" << std::endl;
				std::cout << "number of vertex=" << GRID_VTX_array.length / 3 << std::endl;
			}

			std::cout << "data snips 10 examples:" << std::endl;
			for (UGINT64 i = 0; i < GRID_VTX_array.length / 3; i++)
			{
				if (i > 10)
					break;
				std::cout << GRID_VTX_array[i * 3] << " " << GRID_VTX_array[i * 3 + 1] << " " << GRID_VTX_array[i * 3 + 2] << std::endl;
			}

			//////////// 打印G2NMAP
			PropArray G2NMAP_array;
			bool find_G2NMAP = Rpt.GetStaticData("G2NMAP", G2NMAP_array);
			std::cout << std::endl
					  << "## G2NMAP_array:" << std::endl;
			if (find_G2NMAP)
			{
				std::cout << "Find GRID_VTX, length=" << G2NMAP_array.length << std::endl;
				std::cout << "MAX=" << G2NMAP_array.Max << "\t";
				std::cout << "MIN=" << G2NMAP_array.Min << "\t";
				std::cout << "AVG=" << G2NMAP_array.Avg << "\t" << std::endl;
				std::cout << "data-type=" << G2NMAP_array.type << "\t";
				std::cout << "time-type=" << G2NMAP_array.time_type << "\t";
				std::cout << "time=" << G2NMAP_array.time << "\t";
				std::cout << "data-unit=" << G2NMAP_array.unit << "\t" << std::endl;
			}

			for (UGINT64 i = 0; i < G2NMAP_array.length; i++)
			{
				if (i > 500)
					break;
				std::cout << G2NMAP_array[i] << " ";
				std::cout << std::endl;
			}

			///////////  打印点集做坐标
			std::cout << std::endl
					  << "## NODE_LIST:" << std::endl;
			PropArray NODE_LIST_array;
			bool find_NODE_LIST = Rpt.GetStaticData("NODE_LIST", NODE_LIST_array);
			if (find_NODE_LIST)
			{
				std::cout << "Find NODE_LIST, length=" << NODE_LIST_array.length << std::endl;
				std::cout << "MAX=" << NODE_LIST_array.Max << "\t";
				std::cout << "MIN=" << NODE_LIST_array.Min << "\t";
				std::cout << "AVG=" << NODE_LIST_array.Avg << "\t" << std::endl;
				std::cout << "data-type=" << NODE_LIST_array.type << "\t";
				std::cout << "time-type=" << NODE_LIST_array.time_type << "\t";
				std::cout << "time=" << NODE_LIST_array.time << "\t";
				std::cout << "data-unit=" << NODE_LIST_array.unit << "\t" << std::endl;

				std::cout << "data snips 10 examples:" << std::endl;
				for (UGINT64 i = 0; i < NODE_LIST_array.length / 8; i++)
				{
					if (i > 10)
						break;
					for (UGINT64 j = 0; j < 8; j++)
					{
						std::cout << NODE_LIST_array[i * 8 + j] << " ";
					}
					std::cout << std::endl;
				}
			}

			if (Rpt.GridType() < 3 && find_GRID_VTX && find_NODE_LIST)
			{
				UGINT64 n_visible_elm = NODE_LIST_array.length / 8;
				std::cout << "\nCorner point grid poly data are complete" << std::endl;
				std::cout << "NI=" << Rpt.SGridNI() << "\t";
				std::cout << "NJ=" << Rpt.SGridNJ() << "\t";
				std::cout << "NK=" << Rpt.SGridNK() << "\t";
				std::cout << "NLayer=" << Rpt.SGridNK() / Rpt.PoroLvl() << "\t";
				std::cout << "PoroLvl=" << Rpt.PoroLvl() << "\t";
				std::cout << "number of visible element =length(NODE_LIST)/8 =" << n_visible_elm << "\n"
						  << std::endl;
			}

			PropArray PORV_array;
			PropArray XCOORD_array;
			PropArray YCOORD_array;
			PropArray DEPTH_array;
			PropArray DX_array;
			PropArray DY_array;
			PropArray DZ_array;
			PropArray VOLUME_array;
			PropArray PERMX_array;
			PropArray PERMZ_array;
			PropArray PORO_array;

			bool find_PORV = Rpt.GetStaticData("PORV", PORV_array);
			bool find_XCOORD = Rpt.GetStaticData("XCOORD", XCOORD_array);
			bool find_YCOORD = Rpt.GetStaticData("YCOORD", YCOORD_array);
			bool find_DEPTH = Rpt.GetStaticData("DEPTH", DEPTH_array);
			bool find_DX = Rpt.GetStaticData("DX", DX_array);
			bool find_DY = Rpt.GetStaticData("DY", DY_array);
			bool find_DZ = Rpt.GetStaticData("DZ", DZ_array);
			bool find_VOLUME = Rpt.GetStaticData("VOLUME", VOLUME_array);
			bool find_PERMX = Rpt.GetStaticData("PERMX", PERMX_array);
			bool find_PORO = Rpt.GetStaticData("PORO", PORO_array);

			if ((find_PORV && find_XCOORD && find_YCOORD && find_DEPTH) &&
				(find_DX && find_DY && find_DZ) &&
				(find_VOLUME && find_PERMX) &&
				(find_PORO))
			{
				std::cout << "find all static properties" << std::endl;
			}
			else
			{
				std::cout << "not find all static properties" << std::endl;
			}

			// 保存所有节点信息

			float minx = 1e10;
			float maxx = -1e10;
			float miny = 1e10;
			float maxy = -1e10;
			float minz = 1e10;
			float maxz = -1e10;

			int saveIndex = 1;
			std::ofstream fs_all("../web-vis/public/all_z" + std::to_string(saveIndex) + "c_" + casename + ".csv", std::ios::out | std::ios::app);

			const int xDim = 588;
			const int yDim = 328;
			const int zDim = 560;

			const int targetY = 200;
			std::vector<int> sliceData;

			// for (int z = 0; z < zDim; ++z)
			// {
			// 	for (int x = 0; x < xDim; ++x)
			// 	{
			// 		int index = z * yDim * xDim + targetY * xDim + x;
			// 		sliceData.push_back(G2NMAP_array[index]);
			// 	}
			// }

			// const int targetX = 200;
			// for (int z = 0; z < zDim; ++z) {
			// 	for (int y = 0; y < yDim; ++y) {
			// 		int index = z * yDim * xDim + y * xDim + targetX;
			// 		sliceData.push_back(G2NMAP_array[index]);
			// 	}
			// }

			const int targetZ = 1;
			for (int x = 0; x < xDim; ++x) {
				for (int y = 0; y < yDim; ++y) {
					int index = targetZ * yDim * xDim + y * x + x;
					sliceData.push_back(G2NMAP_array[index]);
				}
			}


			for (int i : sliceData)
			{
				// std::cout << i << " ";
				if (i != -1)
				{
					fs_all << XCOORD_array[i] << "," << YCOORD_array[i] << "," << DEPTH_array[i] << ","
						   << DX_array[i] << "," << DY_array[i] << ","
						   << DZ_array[i] << "," << PORO_array[i] << "," << PORV_array[i] << std::endl;
				}
			}
			std::cout << std::endl;

			fs_all.close();
			std::cout << "save file finish" << std::endl;
		}

		std::cout << "\nPress any key to quit" << std::endl;
	}
	return 0;
}